package protocol.SZ_2021;

import java.util.HashMap;

public class Handle2021SZ15 implements IHandle2021SZ {
    @Override
    public String handle(SZ_2021Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x05:
                return handle1505(cmd);
            case 0x08:
                return handle1508(cmd);
            default:
                return "深标:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle1508(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:月历查询应答");
        builder.append("\r\n");
        builder.append("应答流水号:"+cmd.readWord());
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"成功");
        list.put(1,"失败");
        builder.append("应答结果:"+list.getOrDefault(cmd.readInt(1),"未定义"));
        builder.append("\r\n");

        list.clear();
        list.put(0,"所有");
        list.put(1,"报警");
        builder.append("搜索类型结果:"+list.getOrDefault(cmd.readInt(1),"未定义"));
        builder.append("\r\n");

        int size = cmd.readWord();
        builder.append("搜索总个数:"+size);
        builder.append("\r\n");

        builder.append("年月:"+cmd.readBCD(2));
        builder.append("\r\n");

        int type = cmd.readDWord();
        for (int i = 0;i<31;i++){
            builder.append((i+1)+"日录像状态:"+((type>>i)&0x01));
        }

        builder.append("\r\n");
        return builder.toString();
    }

    private String handle1505(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:终端上传音视频资源列表");
        builder.append("\r\n");
        builder.append("应答流水号:"+cmd.readWord());
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"成功");
        list.put(1,"失败");
        builder.append("应答结果:"+list.getOrDefault(cmd.readInt(1),"未定义"));
        builder.append("\r\n");

        int size = cmd.readDWord();
        builder.append("时间段总数:"+size);
        builder.append("\r\n");

        for (int i = 0;i<size;i++){
            builder.append("逻辑通道号:"+cmd.readInt(1));
            builder.append("\r\n");
            builder.append("起始时间:"+cmd.readInt(6));
            builder.append("\r\n");
            builder.append("结束时间:"+cmd.readInt(6));
            builder.append("\r\n");
            builder.append("报警标志:"+cmd.readBCD(64));
            builder.append("\r\n");
            builder.append("文件大小:"+cmd.readDWord());
            builder.append("\r\n");

            builder.append("文件ID:"+cmd.readString(cmd.readDWord()));
            builder.append("\r\n");
        }

        return builder.toString();
    }
}
